package it.codelair.bakekaservice;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Splash_activity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		final Button buttonStart = (Button) findViewById(R.id.button2);
		buttonStart.setOnClickListener(this);
		final Button buttonTut = (Button) findViewById(R.id.button1);
		buttonTut.setOnClickListener(this);
	}
	
	@Override
    public void onClick(View v) {
        switch ( v.getId() ) {
        
	        case R.id.button1:
	        	
	        	break;
            case R.id.button2:
            	//controllo nonn sia gia loggato con l'account
            	//tre casi
            	//1: ha gia un account su android e vado nella home
            	//2: non ha un account su android ma lo ha su bakekaservice (connessione internet)
            	//3: non ha nessun account: vado alla registrazione
            	AccountManager accountMgr = (AccountManager) getSystemService(ACCOUNT_SERVICE);
            	Account[] accounts = accountMgr.getAccountsByType("it.codelair.bakekaservice");
            	if(accounts!=null && accounts.length>0){
            		//caso 1: vado nella home
            		Intent home = new Intent(getApplicationContext(), PrincipaleActivity.class);
    	            startActivity(home);
            	}
            	else{
            		//casi 2,3
            		//connesisone internet necessaria
            		if(checkConn()){
            			//connessione disponibile
            			//chiedo all'utente se ha gia un'account bakeka service
            			Intent askAccount = new Intent(getApplicationContext(), AskAccount.class);
        	            startActivity(askAccount);
            		}
            		else {
            			Toast t = Toast.makeText(v.getContext(), "Connessione internet non disponibile!", Toast.LENGTH_SHORT);
            			t.show();
            		}
            	}
	            break;
        }
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.splash_activity, menu);
		return true;
	}
	
	public boolean checkConn()
    {
        ConnectivityManager conMgr =  (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null)
          return false;
        if (!i.isConnected())
          return false;
        if (!i.isAvailable())
          return false;
        return true;
    }

}
